<?

  error_reporting(E_ALL);
  ini_set('display_errors', 1);

if (empty($_COOKIE['user'])) {
  header('Location: index.php');
}
if (empty($_GET['id'])) {
  header('Location: dashboard.php');
}

include_once 'controller.inc.php';
$db_link = db_connect();
$cur_user = db_getUserByEmail($_COOKIE['user']);
$event = db_getEvent($_GET['id']);
$event_roles = db_getRolesForEvent($event);
$all_users = db_getAllUsers();
db_disconnect($db_link);

if ($event->getOwnerID() != $cur_user->getID()) {
  header('Location: dashboard.php');
}

$start_datetime = $event->getStartTime();
$end_datetime = $event->getEndTime();

$start_date = $start_datetime->format('m/d/Y');
$start_time = $start_datetime->format('g:i');
$start_afternoon = (int)($start_datetime->format('G')) > 11;

$end_date = $end_datetime->format('m/d/Y');
$end_time = $end_datetime->format('g:i');
$end_afternoon = (int)($end_datetime->format('G')) > 11;

include("header.inc.php");

?>

  <h1>Edit Event</h1>
  
  <form name="new_event" action="update_event.php" method="post" onsubmit="return validate()">
    <p>
      <label for="name">Event Name:</label><br/>
      <input type="text" name="name" id="name" value="<? echo $event->getName() ?>">
    </p>
    <p>
      <label for="owner">Event Manager:</label><br/>
        <select name="owner" id="owner">
<?
  foreach ($all_users as $user) {
    echo '  <option value="'.
         $user->getID().
         '"'.
         ($cur_user->getID() == $user->getID() ? 'selected' : '').
         '>'.
         $user->getName().'</option>';
    echo "\n";
  }
?>
        </select><br/>
      <span class="hint">Warning: If you change this event's manager to someone other than yourself, you will no longer be able to edit the event.</span>
    </p>
    <p>
      <label for="description">Description of Event:</label><br/>
      <textarea name="description" id="description" style="height: 100px; width: 275px;"><? echo $event->getDescription() ?></textarea>
    </p>
    <p>
      <label for="location">Location:</label><br/>
      <input type="text" name="location" id="location" value="<? echo $event->getLocation() ?>">
    </p>
    <p>
      <label for="start_date">Start Date:</label><br/>
      <input type="text" name="start_date" id="start_date" value="<? echo $start_date ?>"><br/>
      <label for="start_time">Start Time:</label><br/>
      <input type="text" name="start_time" id="start_time" size="12" value="<? echo $start_time ?>">
      <select name="start_time_period">
        <option value="0" <? echo ($start_afternoon ? '' : 'selected') ?>>am</option>
        <option value="1" <? echo ($start_afternoon ? 'selected' : '') ?>>pm</option>
      </select>
    </p>
    <p>
      <label for="end_date">End Date:</label><br/>
      <input type="text" name="end_date" id="end_date" value="<? echo $end_date ?>"><br/>
      <label for="end_time">End Time:</label><br/>
      <input type="text" name="end_time" id="end_time" size="12" value="<? echo $end_time ?>">
      <select name="end_time_period">
        <option value="0" <? echo ($end_afternoon ? '' : 'selected') ?>>am</option>
        <option value="1" <? echo ($end_afternoon ? 'selected' : '') ?>>pm</option>
      </select>
    </p>
    
    <h3>Participants:</h3>
    <div id="role_list">
<?
$idx = 1;
foreach ($event_roles as $role) {
  echo '<p id="role'.$idx.'"><input type="text" name="role'.$idx.'_name" value="'.$role->getName().'">';
  echo "\n";
  echo '<select name="role'.$idx.'_user">';
  echo "\n";
  echo '  <option value="0">Nobody Assigned</option>';
  echo "\n";
  foreach ($all_users as $user) {
    echo '  <option value="'.
         $user->getID().
         '"'.
         ($role->getUserID() == $user->getID() ? 'selected' : '').
         '>'.
         $user->getName().'</option>';
    echo "\n";
  }
  echo '</select><a href="#" onclick="return removeRole('.$idx.')"><img src="media/images/delete.png"></a></p>';
  echo "\n";
  
  $idx++;
}
?>
    </div>
    
    <p><a href="#" onclick="return addRole()"><img src="media/images/add.png">Add role</a></p>
    
    <input type="hidden" name="id" value="<? echo $event->getID() ?>">
    
    <input type="submit" name="action" value="Edit Event">
    or
    <a href="view_event.php?id=<? echo $event->getID() ?>">Cancel</a>
  </form>

<script type="text/javascript">
function validate() {
  var form_fields = document.forms['new_event'];
  var name = form_fields['name'].value;
  var start_date = form_fields['start_date'].value.trim();
  var start_time = form_fields['start_time'].value.trim();
  var end_date = form_fields['end_date'].value.trim();
  var end_time = form_fields['end_time'].value.trim();
  
  if (name == null || name == '') {
    alert('Please enter a name.');
    return false;
  }
  if (start_date == null || start_date == '' || 
      !start_date.match(/^\d\d?\/\d\d?\/\d\d(\d\d)?$/)) {
    alert('Please enter a start date as specified.');
    return false;
  }
  if (start_time == null || start_time == '' || 
      !start_time.match(/^\d\d?:\d\d$/)) {
    alert('Please enter a start time as specified.');
    return false;
  }
  if (end_date == null || end_date == '' || 
      !end_date.match(/^\d\d?\/\d\d?\/\d\d(\d\d)?$/)) {
    alert('Please enter an end date as specified.');
    return false;
  }
  if (end_time == null || end_time == '' || 
      !end_time.match(/^\d\d?:\d\d$/)) {
    alert('Please enter an end time as specified.');
    return false;
  }
}

var cur_role = <? echo sizeof($event_roles) + 1 ?>;

function addRole() {
  var role_list = document.getElementById('role_list');
  var new_role = document.createElement('p');
  new_role.setAttribute('id', 'role' + cur_role);
  var role_html = '<input type="text" name="role' + cur_role + '_name"> ';
  role_html += '<select name="role' + cur_role + '_user">';
  role_html += '<option value="0">Nobody Assigned</option>';
<?
foreach ($all_users as $user) {
  echo "\n\n";
  echo "  role_html += '<option value=\"".$user->getID().'">'.$user->getName()."</option>'";
  echo "\n\n";
}
?>
  role_html += '</select><a href="#" onclick="return removeRole(' + cur_role + ')"><img src="media/images/delete.png"></a>';
  new_role.innerHTML = role_html;
  role_list.appendChild(new_role);
  
  cur_role++;
  return false;
}

function removeRole(role_num) {
  var role_list = document.getElementById('role_list');
  var new_role = document.getElementById('role' + role_num);
  role_list.removeChild(new_role);
  
  return false;
}
</script>

<?

include("footer.inc.php");

?>
